<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('采购列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>采购单号：</label>
                                <input type="text" name="orderNo" class="form-control"/>
                            </li>
                            <li>
                                <label>状态</label>
                                <select name="state" class="form-control">
                                    <option value="">所有</option>
                                    <option value="CHECKING">待审核</option>
                                    <option value="CHECK_SUCCESS">审核通过</option>
                                    <option value="CHECK_FAILED">审核不通过</option>
                                    <option value="PURCHASING">采购中</option>
                                    <option value="PURCHASING_SUCCESS">采购完成</option>
                                    <option value="PUTTING_SUCCESS">入库完成</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="addPurchase()" >
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
<!--                <a class="btn btn-warning" onclick="$.table.exportExcel()">-->
<!--                    <i class="fa fa-download"></i> 导出-->
<!--                </a>-->
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "business/purchase";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                detailUrl: prefix + "/detail/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "采购",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'orderNo',
                    title: '采购单号'
                },
                {
                    field: 'money',
                    title: '采购金额'
                },
                {
                    field: 'purUserName',
                    title: '采购人姓名'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'purBeginTime',
                    title: '采购开始时间'
                },
                {
                    field: 'purEndTime',
                    title: '采购结束时间'
                },
                {
                    field: 'state',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return '<span class="badge badge-info" style="'+ getStateCss(value) +'">' + getStateLabel(value) + '</span>';
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="detailFull(\'' + row.id + '\')"><i class="fa fa-search"></i>详情</a> ');
                        if ("NEW" == row.state || "CHECK_FAILED" == row.state){
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="updatePurchase(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        }
                        if ("CHECKING" == row.state){
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="check(\'' + row.id + '\')"><i class="fa fa-adjust"></i>审核</a> ');
                        }

                        if ("CHECK_SUCCESS" == row.state){
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="purchase(\'' + row.id + '\', \'PURCHASING\')"><i class="fa fa-adjust"></i>开始采购</a> ');
                        }
                        if ("CHECK_FAILED" == row.state){
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="failReason(\'' + row.reason + '\')"><i class="fa fa-amazon"></i>不通过原因</a> ');
                        }
                        if ("PURCHASING" == row.state){
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="purchase(\'' + row.id + '\', \'PURCHASING_SUCCESS\')"><i class="fa fa-address-card"></i>采购完成</a> ');
                        }
                        if ("PURCHASING_SUCCESS" == row.state){
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="factory(\'' + row.orderNo + '\')"><i class="fa fa-instagram"></i>入库</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        /** 详情弹窗 */
        function detailFull(id) {
            openFull("采购详情", prefix + '/detail/'+ id)
        }

        // 弹出层全屏
        function openFull(title, url, width, height) {
            // 如果是移动端，就使用自适应大小弹窗
            if ($.common.isMobile()) {
                width = 'auto';
                height = 'auto';
            }
            if ($.common.isEmpty(title)) {
                title = false;
            }
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            }
            if ($.common.isEmpty(width)) {
                width = 800;
            }
            if ($.common.isEmpty(height)) {
                height = ($(window).height() - 50);
            }
            var index = layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: title,
                content: url,
                btn: ['确定', '关闭'],
                // 弹层外区域关闭
                shadeClose: true,
                yes: function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                },
                cancel: function(index) {
                    return true;
                }
            });
            layer.full(index);
        }

        /** 添加弹窗 */
        function addPurchase(){
            addFull("添加采购", prefix + '/add');
        }

        /** 编辑弹窗 */
        function updatePurchase(id){
            addFull("修改采购", prefix + '/edit/' + id);
        }

        function addFull(title, url, width, height) {
            //如果是移动端，就使用自适应大小弹窗
            if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
                width = 'auto';
                height = 'auto';
            }
            if ($.common.isEmpty(title)) {
                title = false;
            }
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            }
            if ($.common.isEmpty(width)) {
                width = 800;
            }
            if ($.common.isEmpty(height)) {
                height = ($(window).height() - 50);
            }
            let index = layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: title,
                content: url,
                btn: ["保存","提交审核","关闭"],
                // 弹层外区域关闭
                shadeClose: false,
                yes: function (index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero, "NEW");
                },
                btn2: function (index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero, "CHECKING");
                    return false;
                },
                cancel: function () {
                    $.table.refresh();
                    return true;
                }
            });
            layer.full(index);
        }

        /** 审核弹窗 */
        function check(id) {
            open("审核页面", prefix + "/check/" + id, 800, 320)
        }

        function open(title, url, width, height, callback, full) {
            //如果是移动端，就使用自适应大小弹窗
            if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
                width = 'auto';
                height = 'auto';
            }
            if ($.common.isEmpty(title)) {
                title = false;
            }
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            }
            if ($.common.isEmpty(width)) {
                width = 800;
            }
            if ($.common.isEmpty(height)) {
                height = ($(window).height() - 50);
            }
            if ($.common.isEmpty(callback)) {
                callback = function (index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            }
            let index = layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: title,
                content: url,
                btn: ['确定', '关闭'],
                // 弹层外区域关闭
                shadeClose: false,
                yes: callback,
                cancel: function (index) {
                    return true;
                }
            });
        }

        /** 审核不通过原因弹窗 */
        function failReason(reason) {
            layer.open({
                title: "审核失败原因【修改后可再次提交】",
                area: ['500px', '200px'],
                content: '<div class="row" style="width: 6000px;  margin-left:7px; margin-top:10px;">'
                    + '<div class="col-sm-12" style="margin-top: 10px">'
                    + '<span>' + reason + '</span>'
                    + '</div>'
                    + '</div>',
                btn: ['关闭'],
                yes: function (index, layerObject) {
                    layer.close(index);
                }
            });
        }

        /** 开始采购 */
        function purchase(id, state) {
            var config = {
                url: prefix + "/purchase",
                type: "post",
                dataType: "json",
                data: {'id': id, 'state':state},
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.closeLoading();
                },
                success: function(result) {
                    if (result.code == 0){
                        $.modal.alertSuccess("操作成功！");
                        $.table.refresh();
                    } else{
                        $.modal.alertError("操作失败，请重试！");
                        $.table.refresh();
                    }
                }
            };
            $.ajax(config)
        }

        /**  打开仓库列表 */
        var pCode;    // 需要入库的采购id
        function factory(orderNo) {
            pCode = orderNo;
            openFull('选择仓库', prefix + '/factory');
        }

        /** 执行入库操作 */
        function setFactory(id) {
            // id为选择的仓库id
            console.log(id + "," + pCode)
            var config = {
                url: prefix + "/factory",
                type: "post",
                dataType: "json",
                data: {'pCode': pCode, 'fId':id},
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.closeLoading();
                },
                success: function(result) {
                    if (result.code == 0){
                        $.modal.alertSuccess("操作成功！");
                        $.table.refresh();
                    } else{
                        $.modal.alertError("操作失败，请重试！");
                        $.table.refresh();
                    }
                }
            };
            $.ajax(config)
        }
    </script>
</body>
</html>